/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Manish Mehta 
 												
Class							:				Entry
 
Function						: 				A simple class for holding the trace entry
****************************************************************************/

package trace;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.List;

import org.w3c.dom.Node;

import planninglanguage.conditions.Condition;
import planninglanguage.plans.Plan;
import base.CBLILRModule;
import base.WargusGameState;

public class Entry implements Serializable{
	public static enum EventType {start, succeed, fail};
	public Plan pl;
	public Entry.EventType evtype;
	public transient WargusGameState gameState;
	public List <Condition> condlist = null;
	public double delay = 0.0;
	public double time = 0.0;
	int length = 0;
	Trace.FailureConditionType conditiontype;
	public Entry(WargusGameState a_gameState, Plan pentry, Entry.EventType ev, double curtime, double curdelay, List<Condition> list, Trace.FailureConditionType condtype)
	{
		gameState = a_gameState;
		pl = pentry;
		evtype = ev;
		time = curtime;
		delay = curdelay;
		condlist = list;
		conditiontype = condtype;
	
		
	}
	
	// TODO use XMLWriter
	private void writeObject(java.io.ObjectOutputStream out) throws IOException, ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		out.defaultWriteObject();
		out.writeObject("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <gamestate xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"Wargus-trace.xsd\">");
		// TODO: save this again!!!
		//		if (gameState!=null) gameState.writeXML(out);
		out.writeObject("</gamestate>");
		out.flush();

	
	}
	private void readObject(java.io.ObjectInputStream in) throws IOException,
	    ClassNotFoundException, ClassCastException, InstantiationException, IllegalAccessException
	{
		in.defaultReadObject();
		String str = (String)in.readObject();
		StringReader rd = new StringReader(str);
		Node e = CBLILRModule.ReadXMLfromReader(rd);
		gameState = WargusGameState.ImportXMLGameState(e,0);

	
	}

	public List<Condition> getCondlist()
	{
		return condlist;
	}
	public double getDelay()
	{
		return delay;
	}
	public double getTime()
	{
		return time;
	}
	public WargusGameState getGameState()
	{
		return gameState;
	}
	public Plan getPlan()
	{
		return pl;
	}
	public Entry.EventType getEventType()
	{
		return evtype;
	}
	public Trace.FailureConditionType getfailedConditionType()
	{
		return conditiontype;
	}
	public void setfailedConditionType(Trace.FailureConditionType condtype)
	{
		conditiontype = condtype;
	}
	
	public void setCondlist(List<Condition> c)
	{
		condlist =c;
	}
	public void setDelay(double d)
	{
		delay = d;
	}
	public void setTime(double d)
	{
		time = d;
	}
	public void setGameState(WargusGameState n)
	{
		gameState = n;
	}
	public void setPlan(Plan p)
	{
		pl = p;
	}
	public void setEventType(Entry.EventType ev)
	{
		evtype = ev;
	}
	
}
